 //控制层 
app.controller('itemCatController' ,function($scope,$controller,typeTemplateService ,itemCatService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		itemCatService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		itemCatService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		itemCatService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=itemCatService.update( $scope.entity ); //修改  
		}else{
            $scope.entity.parentId=$scope.parentId;//赋予上级 ID
			serviceObject=itemCatService.add( $scope.entity  );//增加
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.findByParendId($scope.entity.parentId)//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		itemCatService.dele( $scope.deleteIds ).success(
			function(response){
				if(response.success){
                    $scope.findByParendId($scope.parentId)//重新加载
					$scope.deleteIds=[];
				}else {
					alert(response.message);
				}
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		itemCatService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    $scope.parentId=0;
	//根据父id查找此父id下的所有子分类列表
	$scope.findByParendId=(parentId)=>{
		$scope.parentId=parentId;
		itemCatService.findByParendId(parentId).success(response=>{
			$scope.list=response
		})
	}
	//定义当前级别
	$scope.level=1;
	//设置分类级别
	$scope.setLevel=(level)=>{
		$scope.level=level;
	}
	//调用该方法判断当前级别
	$scope.selectList=(itemcat)=>{
		if($scope.level==1){
			$scope.entity1=itemcat;
			$scope.entity2=null;
			$scope.entity3=null;
		}else if($scope.level==2){
            $scope.entity2=itemcat;
            $scope.entity3=null;
		}else if($scope.level==3){
            $scope.entity3=itemcat;
		}
        //根据父id查找此父id下的所有子分类列表
		$scope.findByParendId(itemcat.id);
	}
	//查找所有模板
	$scope.findAllType=()=>{
		typeTemplateService.findAll().success(response=>{
			$scope.typeList=response;
		})
	}

});	
